The goals / steps of this project are the following:
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import glob
%matplotlib inline
def find_object_image_sets(path_str, nx, ny, show_img=False):
object_sets = []
image_sets = []
# Generate a matrics, have nx * ny rows, 3 colomns, type is np.float32
object_points = np.zeros((nx * ny, 3), np.float32)
object_points[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)
# Load all chessboard images path
chessboard_imgs_path = glob.glob(path_str)
for chessboard_img_path in chessboard_imgs_path:
# Load image
img = cv2.imread(chessboard_img_path)
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
if ret == True:
object_sets.append(object_points)
image_sets.append(corners)
# Draw the corners on the chessboard
if show_img == True:
cv2.drawChessboardCorners(img, (nx, ny), corners, ret)
f, ax = plt.subplots(1, 1, figsize=(7, 7))
ax.imshow(img)
ax.axis('off')
ax.set_title(chessboard_img_path, fontsize = 20)
size = (mpimg.imread(chessboard_imgs_path[0]).shape[1], mpimg.imread(chessboard_imgs_path[0]).shape[0])
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(object_sets, image_sets, size, None, None)
return (mtx, dist)
chessboard_imgs_path = "./camera_cal/*.jpg"
nx = 9 # The number of inside corners in x
ny = 6 # The number of inside corners in y
calibration_matrix, distortion_coefficients = find_object_image_sets(chessboard_imgs_path, nx, ny, True)
def undistort_image(image, calibration_matrix, distortion_coefficients):
undistorted_image = cv2.undistort(image, calibration_matrix, distortion_coefficients, None, calibration_matrix)
return undistorted_image
# Load all chessboard images
chessboard_imgs_list = glob.glob(chessboard_imgs_path)
for chessboard_img in chessboard_imgs_list:
original_image = cv2.imread(chessboard_img)
undistorted_image = undistort_image(original_image, calibration_matrix, distortion_coefficients)
fig, axes = plt.subplots(1, 2, figsize=(20,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(original_image)
axes[0].set_title('Image before calibration', fontsize = 20)
axes[1].imshow(undistorted_image)
axes[1].set_title('Image after calibration', fontsize = 20)
# Load all test images
road_imgs_path = "test_images/*.jpg"
road_imgs_list = glob.glob(road_imgs_path)
original_images = []
undistorted_images = []
for road_img in road_imgs_list:
original_image = cv2.imread(road_img)
original_images.append(original_image)
undistorted_image = undistort_image(original_image, calibration_matrix, distortion_coefficients)
undistorted_images.append(undistorted_image)
fig, axes = plt.subplots(1, 2, figsize=(20,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
axes[0].set_title('Image before calibration', fontsize = 20)
axes[1].imshow(cv2.cvtColor(undistorted_image, cv2.COLOR_BGR2RGB))
axes[1].set_title('Image after calibration', fontsize = 20)
# The function that takes an image, gradient orientation, and threshold min / max values.
def abs_sobel_thresh(img, orient='x', thresh=(0,255)):
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Apply x or y gradient with the OpenCV Sobel() function
# and take the absolute value
if orient == 'x':
abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0))
if orient == 'y':
abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1))
# Rescale back to 8 bit integer
scaled_sobel = np.uint8(255*abs_sobel/np.max(abs_sobel))
# Create a copy and apply the threshold
binary_output = np.zeros_like(scaled_sobel)
# Here I'm using inclusive (>=, <=) thresholds, but exclusive is ok too
binary_output[(scaled_sobel >= thresh[0]) & (scaled_sobel <= thresh[1])] = 1
# Return the result
return binary_output
# The function to return the magnitude of the gradient for a given sobel kernel size and threshold values
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)):
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Take both Sobel x and y gradients
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
# Calculate the gradient magnitude
gradmag = np.sqrt(sobelx**2 + sobely**2)
# Rescale to 8 bit
scale_factor = np.max(gradmag)/255
gradmag = (gradmag/scale_factor).astype(np.uint8)
# Create a binary image of ones where threshold is met, zeros otherwise
binary_output = np.zeros_like(gradmag)
binary_output[(gradmag >= mag_thresh[0]) & (gradmag <= mag_thresh[1])] = 1
# Return the binary image
return binary_output
# The function to threshold an image for a given range and Sobel kernel
def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi/2)):
# Grayscale
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# Calculate the x and y gradients
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
# Take the absolute value of the gradient direction,
# apply a threshold, and create a binary image result
absgraddir = np.arctan2(np.absolute(sobely), np.absolute(sobelx))
binary_output = np.zeros_like(absgraddir)
binary_output[(absgraddir >= thresh[0]) & (absgraddir <= thresh[1])] = 1
# Return the binary image
return binary_output
def gradient_threshold(img, ksize=15, sthresh=(20, 100), mthresh =(20, 100), dthresh=(0.7, 1.3)):
gradx = abs_sobel_thresh(img, orient='x', thresh=sthresh)
grady = abs_sobel_thresh(img, orient='y', thresh=sthresh)
mag_binary = mag_thresh(img, sobel_kernel=ksize, mag_thresh=mthresh)
dir_binary = dir_threshold(img, sobel_kernel=ksize, thresh=dthresh)
combined_binary = np.zeros_like(dir_binary)
combined_binary[((gradx == 1) & (grady == 1)) | ((mag_binary == 1) & (dir_binary == 1))] = 1
return combined_binary
after_gradient_thresholding_imgs = []
for i in np.arange(0, len(original_images)):
after_gradient_thresholding = gradient_threshold(undistorted_images[i])
after_gradient_thresholding_imgs.append(after_gradient_thresholding)
fig, axes = plt.subplots(1, 3, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
axes[0].set_title('Original Image', fontsize = 20)
axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
axes[1].set_title('Image after calibration', fontsize = 20)
axes[2].imshow(after_gradient_thresholding_imgs[i], cmap='gray')
axes[2].set_title('Image after apply gradient thresholding', fontsize = 20)
# The function that thresholds the S-channel of HLS
def s_channel_HLS(img, thresh=(0, 255)):
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
s_channel = hls[:,:,2]
binary_output = np.zeros_like(s_channel)
binary_output[(s_channel > thresh[0]) & (s_channel <= thresh[1])] = 1
return binary_output
# The function that thresholds the L-channel of LUV
def l_channel_LUV(img, thresh=(0, 255)):
luv = cv2.cvtColor(img, cv2.COLOR_BGR2LUV)
l_channel = luv[:,:,0]
binary_output = np.zeros_like(l_channel)
binary_output[(l_channel > thresh[0]) & (l_channel <= thresh[1])] = 1
return binary_output
# The function that thresholds the L & B channel of LAB
def lb_channel_LAB(img, lthresh=(0, 255), bthresh=(0,255)):
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_channel = lab[:,:,0]
b_channel = lab[:,:,2]
l_binary_output = np.zeros_like(l_channel)
l_binary_output[(l_channel > lthresh[0]) & (l_channel <= lthresh[1])] = 1
b_binary_output = np.zeros_like(b_channel)
b_binary_output[(b_channel > bthresh[0]) & (b_channel <= bthresh[1])] = 1
combined_binary = np.zeros_like(b_channel)
combined_binary[(l_binary_output == 1) | (b_binary_output == 1)] = 1
return combined_binary
def color_thresholding(img):
# I finally didn't use HLS, since it has more noise
luv_binary = l_channel_LUV(img, thresh=(210, 255))
lab_binary = lb_channel_LAB(img, lthresh=(230, 255), bthresh=(155,255))
combined_binary = np.zeros_like(lab_binary)
combined_binary[(luv_binary == 1) | (lab_binary == 1)] = 1
return combined_binary
after_color_thresholding_imgs = []
for i in np.arange(0, len(original_images)):
after_color_thresholding = color_thresholding(undistorted_images[i])
after_color_thresholding_imgs.append(after_color_thresholding)
fig, axes = plt.subplots(1, 4, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
axes[0].set_title('Original Image', fontsize = 20)
axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
axes[1].set_title('Image after calibration', fontsize = 20)
axes[2].imshow(after_gradient_thresholding_imgs[i], cmap='gray')
axes[2].set_title('Image after apply gradient thresholding', fontsize = 20)
axes[3].imshow(after_color_thresholding_imgs[i], cmap='gray')
axes[3].set_title('Image after apply color thresholding', fontsize = 20)
mask = np.zeros_like(after_gradient_thresholding_imgs[0])
img_tmp = np.copy(original_images[0])
height = 720
length = 1280
left_down = (230, height - 25)
left_top = (580, 445)
right_top = (680, 445)
right_down = (length - 110, height - 25)
trapezoid_outter = np.array([[left_down, left_top, right_top, right_down]])
cv2.fillPoly(mask, trapezoid_outter, 255)
result = cv2.polylines(img_tmp, [trapezoid_outter], True, (0,255,255), 3)
plt.imshow(result)
mask = np.zeros_like(after_gradient_thresholding_imgs[0])
img_tmp = np.copy(original_images[0])
height = 720
length = 1280
left_down = (350, height - 25)
left_top = (580, 480)
right_top = (700, 480)
right_down = (length - 220, height - 25)
trapezoid_inner = np.array([[left_down, left_top, right_top, right_down]])
cv2.fillPoly(mask, trapezoid_inner, 255)
result = cv2.polylines(img_tmp, [trapezoid_inner], True, (0,255,255), 3)
plt.imshow(result)
def region_of_interest(img, vertices_out, vertices_in):
"""
Applies an image mask.
Only keeps the region of the image defined by the polygon
formed from `vertices`. The rest of the image is set to black.
`vertices` should be a numpy array of integer points.
"""
#defining a blank mask to start with
mask = np.zeros_like(img)
#filling pixels inside the polygon defined by "vertices" with the fill color
cv2.fillPoly(mask, vertices_out, 1)
#returning the image only where mask pixels are nonzero
masked_image = cv2.bitwise_and(img, mask)
cv2.fillPoly(masked_image, vertices_in, 0)
return masked_image
masked_edges = region_of_interest(after_gradient_thresholding_imgs[0], trapezoid_outter, trapezoid_inner)
plt.imshow(masked_edges, cmap = 'gray')
def thresholding_with_mask(img, trapezoid_out, trapezoid_in):
color_thresh = color_thresholding(img)
gradient_thresh = gradient_threshold(img)
combined_binary = np.zeros_like(gradient_thresh)
combined_binary[(color_thresh == 1) | (gradient_thresh == 1)] = 1
after_mask = region_of_interest(combined_binary, trapezoid_out, trapezoid_in)
return after_mask
after_thresholding_imgs = []
for i in np.arange(0, len(original_images)):
after_thresholding = thresholding_with_mask(undistorted_images[i], trapezoid_outter, trapezoid_inner)
after_thresholding_imgs.append(after_thresholding)
fig, axes = plt.subplots(1, 3, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
axes[0].set_title('Original Image', fontsize = 20)
axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
axes[1].set_title('Image after calibration', fontsize = 20)
axes[2].imshow(after_thresholding_imgs[i], cmap='gray')
axes[2].set_title('Image after apply thresholding', fontsize = 20)
mask = np.zeros_like(after_gradient_thresholding_imgs[0])
img_tmp = np.copy(original_images[2])
height = 720
length = 1280
left_down = (210,height)
left_top = (595,450)
right_top = (690,450)
right_down = (1110, height)
original_area = np.array([[left_down, left_top, right_top, right_down]])
cv2.fillPoly(mask, original_area, 255)
result = cv2.polylines(img_tmp, [original_area], True, (0,255,255), 3)
plt.figure(figsize = (16,8))
plt.imshow(result)
def calculate_M_Minv():
height = 720
length = 1280
# Four source coordinates
src = np.float32([
[210,height],
[595,450],
[690,450],
[1110, height]
])
# Four desired coordinates
dst = np.float32([
[200, height],
[200, 0],
[1000, 0],
[1000, height]
])
# Compute the perspective transform matrix, M
M = cv2.getPerspectiveTransform(src, dst)
Minv = cv2.getPerspectiveTransform(dst, src)
return M, Minv
M, Minv = calculate_M_Minv()
def warp(img, M):
img_size = (img.shape[1], img.shape[0])
warped = cv2.warpPerspective(img, M, img_size)
return warped
after_perspective_trans_imgs = []
after_perspective_trans_imgs_color = []
for i in np.arange(0, len(original_images)):
after_perspective_trans = warp(after_thresholding_imgs[i], M)
after_perspective_trans_imgs.append(after_perspective_trans)
after_perspective_trans_color = warp(undistorted_images[i], M)
after_perspective_trans_imgs_color.append(after_perspective_trans_color)
fig, axes = plt.subplots(1, 5, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(cv2.cvtColor(original_images[i], cv2.COLOR_BGR2RGB))
axes[0].set_title('Original Image', fontsize = 20)
axes[1].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
axes[1].set_title('Image after calibration', fontsize = 20)
axes[2].imshow(after_perspective_trans_imgs_color[i], cmap='gray')
axes[2].set_title('(Color image perspective trans)', fontsize = 20)
axes[3].imshow(after_thresholding_imgs[i], cmap='gray')
axes[3].set_title('Image after thresholding', fontsize = 20)
axes[4].imshow(after_perspective_trans_imgs[i], cmap='gray')
axes[4].set_title('Image after perspective trans', fontsize = 20)
def hist(img):
# Grab only the bottom half of the image
# Lane lines are likely to be mostly vertical nearest to the car
# Sum across image pixels vertically - make sure to set an `axis`
# i.e. the highest areas of vertical lines should be larger values
histogram_t = np.sum(img[img.shape[0]//2:,:], axis=0)
return histogram_t
histogram_imgs = []
for i in np.arange(0, len(original_images)):
histogram = hist(after_perspective_trans_imgs[i])
histogram_imgs.append(histogram)
fig, axes = plt.subplots(1, 2, figsize=(35,10))
axes[0].imshow(after_perspective_trans_imgs[i], cmap='gray')
axes[0].set_title('Image after perspective trans', fontsize = 40)
axes[1].plot(histogram_imgs[i])
axes[1].set_title('Histo of perspective trans', fontsize = 40)
def find_lane_pixels(binary_warped):
# Take a histogram of the bottom half of the image
histogram = np.sum(binary_warped[binary_warped.shape[0]//2:,:], axis=0)
# Create an output image to draw on and visualize the result
out_img = np.dstack((binary_warped, binary_warped, binary_warped))
# Find the peak of the left and right halves of the histogram
# These will be the starting point for the left and right lines
midpoint = np.int(histogram.shape[0]//2)
leftx_base = np.argmax(histogram[:midpoint])
rightx_base = np.argmax(histogram[midpoint:]) + midpoint
# HYPERPARAMETERS
# Choose the number of sliding windows
nwindows = 9
# Set the width of the windows +/- margin
margin = 100
# Set minimum number of pixels found to recenter window
minpix = 50
# Set height of windows - based on nwindows above and image shape
window_height = np.int(binary_warped.shape[0]//nwindows)
# Identify the x and y positions of all nonzero pixels in the image
nonzero = binary_warped.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])
# Current positions to be updated later for each window in nwindows
leftx_current = leftx_base
rightx_current = rightx_base
# Create empty lists to receive left and right lane pixel indices
left_lane_inds = []
right_lane_inds = []
# Step through the windows one by one
for window in range(nwindows):
# Identify window boundaries in x and y (and right and left)
win_y_low = binary_warped.shape[0] - (window+1)*window_height
win_y_high = binary_warped.shape[0] - window*window_height
win_xleft_low = leftx_current - margin
win_xleft_high = leftx_current + margin
win_xright_low = rightx_current - margin
win_xright_high = rightx_current + margin
# Draw the windows on the visualization image
cv2.rectangle(out_img,
(win_xleft_low, win_y_low),
(win_xleft_high, win_y_high),
(0, 255,0),
8)
cv2.rectangle(out_img,
(win_xright_low, win_y_low),
(win_xright_high, win_y_high),
(0, 255, 0),
8)
# Identify the nonzero pixels in x and y within the window #
good_left_inds = ((nonzeroy >= win_y_low) &
(nonzeroy < win_y_high) &
(nonzerox >= win_xleft_low) &
(nonzerox < win_xleft_high)).nonzero()[0]
good_right_inds = ((nonzeroy >= win_y_low) &
(nonzeroy < win_y_high) &
(nonzerox >= win_xright_low) &
(nonzerox < win_xright_high)).nonzero()[0]
# Append these indices to the lists
left_lane_inds.append(good_left_inds)
right_lane_inds.append(good_right_inds)
# If you found > minpix pixels, recenter next window on their mean position
if len(good_left_inds) > minpix:
leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
if len(good_right_inds) > minpix:
rightx_current = np.int(np.mean(nonzerox[good_right_inds]))
left_lane_inds = np.concatenate(left_lane_inds)
right_lane_inds = np.concatenate(right_lane_inds)
# Extract left and right line pixel positions
leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]
return leftx, lefty, rightx, righty, out_img
def fit_polynomial(binary_warped):
# Find our lane pixels first
leftx, lefty, rightx, righty, out_img = find_lane_pixels(binary_warped)
# Fit a second order polynomial to each using `np.polyfit`
left_fit = np.polyfit(lefty, leftx, 2)
right_fit = np.polyfit(righty, rightx, 2)
# Generate x and y values for plotting
ploty = np.linspace(0, binary_warped.shape[0]-1, binary_warped.shape[0] )
left_fitx = left_fit[0]*ploty**2 + left_fit[1]*ploty + left_fit[2]
right_fitx = right_fit[0]*ploty**2 + right_fit[1]*ploty + right_fit[2]
## Visualization ##
# Colors in the left and right lane regions
out_img[lefty, leftx] = [255, 0, 0]
out_img[righty, rightx] = [255, 0, 0]
return out_img, left_fitx, right_fitx, ploty, leftx, lefty, rightx, righty
plt.figure(figsize = (16,8))
plt.imshow(after_perspective_trans_imgs[0], cmap='gray')
plt.figure(figsize = (16,8))
plt.imshow(fit_polynomial(after_perspective_trans_imgs[0].astype(np.uint8))[0]) # TODO need to transform to uint8
after_find_line_imgs = []
for i in np.arange(0, len(original_images)):
after_find_line, _, _, _, _, _, _, _ = fit_polynomial(after_perspective_trans_imgs[i].astype(np.uint8))
after_find_line_imgs.append(after_find_line)
fig, axes = plt.subplots(1, 4, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
axes[0].set_title('Image after calibration', fontsize = 20)
axes[1].imshow(after_perspective_trans_imgs_color[i])
axes[1].set_title('(Color image perspective trans)', fontsize = 20)
axes[2].imshow(after_perspective_trans_imgs[i], cmap='gray')
axes[2].set_title('Image after perspective trans', fontsize = 20)
axes[3].imshow(after_find_line_imgs[i])
axes[3].set_title('Image after find lane', fontsize = 20)
def draw_lines(undist, warped, left_fitx, right_fitx, ploty, Minv):
# Create an image to draw the lines on
warp_zero = np.zeros_like(warped).astype(np.uint8)
color_warp = np.dstack((warp_zero, warp_zero, warp_zero))
# Recast the x and y points into usable format for cv2.fillPoly()
pts_left = np.array([np.transpose(np.vstack([left_fitx, ploty]))])
pts_right = np.array([np.flipud(np.transpose(np.vstack([right_fitx, ploty])))])
pts = np.hstack((pts_left, pts_right))
# Draw the lane onto the warped blank image
cv2.fillPoly(color_warp, np.int_([pts]), (255, 102, 0))
# Warp the blank back to original image space using inverse perspective matrix (Minv)
newwarp = cv2.warpPerspective(color_warp, Minv, (undist.shape[1], undist.shape[0]))
# Combine the result with the original image
result = cv2.addWeighted(undist, 1, newwarp, 0.7, 0)
return result
plt.figure(figsize = (16,8))
after_find_line, left_fitx, right_fitx, ploty, _, _, _, _ = fit_polynomial(after_perspective_trans_imgs[0].astype(np.uint8))
t3 = draw_lines(undistorted_images[0], after_perspective_trans_imgs[0], left_fitx, right_fitx, ploty, Minv)
plt.imshow(cv2.cvtColor(t3, cv2.COLOR_BGR2RGB))
result_img = []
for i in np.arange(0, len(original_images)):
after_find_line, left_fitx, right_fitx, ploty, _, _, _, _ = fit_polynomial(after_perspective_trans_imgs[i].astype(np.uint8))
result = draw_lines(undistorted_images[0], after_perspective_trans_imgs[0], left_fitx, right_fitx, ploty, Minv)
result_img.append(result)
fig, axes = plt.subplots(1, 4, figsize=(30,10), subplot_kw={'xticks':[], 'yticks':[]})
axes[0].imshow(cv2.cvtColor(undistorted_images[i], cv2.COLOR_BGR2RGB))
axes[0].set_title('Image after calibration', fontsize = 20)
axes[1].imshow(after_perspective_trans_imgs_color[i])
axes[1].set_title('(Color image perspective trans)', fontsize = 20)
axes[2].imshow(after_find_line_imgs[i])
axes[2].set_title('Image after find lane', fontsize = 20)
axes[3].imshow(cv2.cvtColor(result_img[i], cv2.COLOR_BGR2RGB))
axes[3].set_title('Final result', fontsize = 20)
def measure_curvature_real(leftx, lefty, rightx, righty):
'''
Calculates the curvature of polynomial functions in meters.
'''
# Define conversions in x and y from pixels space to meters
ym_per_pix = 30/720 # meters per pixel in y dimension
xm_per_pix = 3.7/700 # meters per pixel in x dimension
left_fit_cr = np.polyfit(lefty * ym_per_pix, leftx * xm_per_pix, 2)
right_fit_cr = np.polyfit(righty * ym_per_pix, rightx * xm_per_pix, 2)
# Define y-value where we want radius of curvature
# We'll choose the maximum y-value, corresponding to the bottom of the image
y_eval = 720
# Calculation of R_curve (radius of curvature)
left_curverad = ((1 + (2 * left_fit_cr[0] * y_eval * ym_per_pix + left_fit_cr[1])**2)**1.5) / np.absolute(2 * left_fit_cr[0])
right_curverad = ((1 + (2 * right_fit_cr[0] * y_eval * ym_per_pix + right_fit_cr[1])**2)**1.5) / np.absolute(2 * right_fit_cr[0])
radius = (left_curverad + right_curverad) / 2
offset = (640 - (leftx[-1] + rightx[-1]) / 2) * xm_per_pix
return radius, offset
# Calculate the radius of curvature in meters for both lane lines
_, _, _, _, leftx, lefty, rightx, righty = fit_polynomial(after_perspective_trans_imgs[0].astype(np.uint8))
radius, offset = measure_curvature_real(leftx, lefty, rightx, righty)
print(radius, 'm', offset, 'm')
def add_text(img, radius, offset):
cv2.putText(img,
'Radius of curvature of the lane: {}(m)'.format(round(radius, 3)),
(500, 20 * 3),
fontFace = 16,
fontScale = 1,
color=(255, 255, 255),
thickness = 2)
cv2.putText(img,
'Offset from center: {}(m)'.format(round(offset, 3)),
(500, 20 * 5),
fontFace = 16,
fontScale = 1,
color=(255, 255, 255),
thickness = 2)
return img
def process_image(image1):
image2 = undistort_image(image1, calibration_matrix, distortion_coefficients)
image3 = thresholding_with_mask(image2, trapezoid_outter, trapezoid_inner)
image4 = after_perspective_trans = warp(image3, M)
image41 = warp(image2, M)
image5, left_fitx, right_fitx, ploty, leftx, lefty, rightx, righty = fit_polynomial(image4.astype(np.uint8))
radius, offset = measure_curvature_real(leftx, lefty, rightx, righty)
image6 = draw_lines(image2, image4, left_fitx, right_fitx, ploty, Minv)
small_1 = cv2.resize(image5, (int(image5.shape[1] / 4), int(image5.shape[0] / 4)))
x_offset_1 = 30
y_offset_1 = 30
result = add_text(image6, radius, offset)
result[y_offset_1 : y_offset_1 + small_1.shape[0], x_offset_1 : x_offset_1 + small_1.shape[1]] = small_1
small_2 = cv2.resize(image41, (int(image41.shape[1] / 4), int(image41.shape[0] / 4)))
x_offset_2 = 30
y_offset_2 = 60 + small_1.shape[0]
result[y_offset_2 : y_offset_2 + small_2.shape[0], x_offset_2 : x_offset_2 + small_2.shape[1]] = small_2
#result = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
return result
result = process_image(cv2.imread('test_images/test1.jpg'))
plt.figure(figsize = (16,8))
plt.imshow(result)
from moviepy.editor import VideoFileClip
from IPython.display import HTML
# Using VideoFileClip('project_video.mp4').subclip(0,5) to generate a 5 seconds subclip for test
output = 'project_video_output.mp4'
clip = VideoFileClip('project_video.mp4')
output_clip = clip.fl_image(process_image)
%time output_clip.write_videofile(output, audio=False)
HTML("""
<video width="640" height="360" controls>
<source src="{0}">
</video>
""".format(output))